import Vue from 'vue'
// import moment from 'moment'
import format from 'date-fns/format'
// 自定义过滤器
Vue.filter('date-ytd', function(value, formatStr = 'YYYY-MM-DD') {
  return format(value, formatStr)
})

Vue.filter('date-format', function(value, formatStr = 'MM-DD') {
  return format(value, formatStr)
})

Vue.filter('date-format-yyyy', function(value, formatStr = 'YYYY') {
  return format(value, formatStr)
})

Vue.filter('date-M-D', function(value, formatStr = 'M月D日') {
  return format(value, formatStr)
})

// 限制文字长度
Vue.filter('ellipsis', function(value, txtLength) {
  if (!value) return ''
  if (value.length > txtLength) {
    return value.slice(0, txtLength) + '...'
  }
  return value
})

const YTD = () => {
  // 获取当前时间
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  const nowDate = year + '-' + month + '-' + day

  return nowDate
}

const Weeks = () => {
  let str = ''
  const week = new Date().getDay()
  if (week === 0) {
    str = '星期日'
  } else if (week === 1) {
    str = '星期一'
  } else if (week === 2) {
    str = '星期二'
  } else if (week === 3) {
    str = '星期三'
  } else if (week === 4) {
    str = '星期四'
  } else if (week === 5) {
    str = '星期五'
  } else if (week === 6) {
    str = '星期六'
  }
  return str
}

// 只有年月日
const formatDate = (secs) => {
  var t = new Date(secs)
  var year = t.getFullYear()
  var month = t.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  var date = t.getDate()
  if (date < 10) {
    date = '0' + date
  }
  var hour = t.getHours()
  if (hour < 10) {
    hour = '0' + hour
  }
  var minute = t.getMinutes()
  if (minute < 10) {
    minute = '0' + minute
  }
  var second = t.getSeconds()
  if (second < 10) {
    second = '0' + second
  }
  return year + '-' + month + '-' + date
}
// 年月日时分
const formatDateMin = (secs) => {
  var t = new Date(secs)
  var year = t.getFullYear()
  var month = t.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  var date = t.getDate()
  if (date < 10) {
    date = '0' + date
  }
  var hour = t.getHours()
  if (hour < 10) {
    hour = '0' + hour
  }
  var minute = t.getMinutes()
  if (minute < 10) {
    minute = '0' + minute
  }
  var second = t.getSeconds()
  if (second < 10) {
    second = '0' + second
  }
  return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
}

//  https://blog.csdn.net/qq_35430000/article/details/82183079
export {
  formatDate,
  formatDateMin,
  YTD,
  Weeks
}
